//
//  ShoppingCarBottomView.swift
//  sss
//
//  Created by Mac on 2021/1/13.
//  Copyright © 2021 hrrMac. All rights reserved.
//

import UIKit

class ShoppingCarBottomView: UIView {
    /// 选择按钮
    let selectBtn = UIButton()
    /// 选择按钮的文字提示
    let allSelectLab = UILabel()
    /// 合计
    let totalTagLab = UILabel()
    /// 合计的钱数
    let moneyLab = UILabel()
    /// 结算按钮
    let settlementBtn = UIButton()
    /// 顶部分隔线
    let topLine = UIView()
    /// 底部分隔线
    let bottomLine = UIView()
    
    /// 全选按钮事件 【是否全选】
    var touchAllSelectEvent: ((Bool)->Void)?
    
    /// 点击结算按钮 【flase 说明没有商品可以结算】
    var touchSettlementEvent: ((Bool)->Void)?
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        configUI()
    }
    
    
    private func configUI(){
        // 选择按钮正常状态的图片
        selectBtn.setImage(UIImage(named: "sc_select_no"), for: .normal)
        // 选择按钮选中状态下的图片
        selectBtn.setImage(UIImage(named: "sc_select_yes"), for: .selected)
        selectBtn.imageView?.contentMode = .scaleAspectFit
        selectBtn.addTarget(self, action: #selector(clickSelectBtn), for: .touchUpInside)
        
        allSelectLab.textColor = UIColor(red: 0.4, green: 0.4, blue: 0.4,alpha:1)
        allSelectLab.font = UIFont.systemFont(ofSize: 11)
        allSelectLab.text = "全选"
        
        totalTagLab.textColor = UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)
        totalTagLab.font = UIFont.systemFont(ofSize: 16)
        totalTagLab.text = "合计"
        
        moneyLab.textColor = UIColor(red: 1, green: 0.75, blue: 0.15, alpha: 1)
        moneyLab.font = UIFont.systemFont(ofSize: 16)
        moneyLab.text = "¥ 0"
        
        settlementBtn.backgroundColor = UIColor(red: 1, green: 0.75, blue: 0.15, alpha: 1)
        settlementBtn.titleLabel?.textColor = UIColor.white
        settlementBtn.layer.cornerRadius = 3
        settlementBtn.clipsToBounds = true
        settlementBtn.setTitle("结算(0)", for: .normal)
        settlementBtn.addTarget(self, action: #selector(clickSettlementBtn), for: .touchUpInside)

        topLine.backgroundColor = UIColor(red: 0.4, green: 0.4, blue: 0.4,alpha:1)
        bottomLine.backgroundColor = UIColor(red: 0.4, green: 0.4, blue: 0.4,alpha:1)
        
        addSubview(selectBtn)
        addSubview(allSelectLab)
        addSubview(totalTagLab)
        addSubview(moneyLab)
        addSubview(settlementBtn)
        addSubview(topLine)
        addSubview(bottomLine)
        refreshFrame()
        
    }
    
    // 刷新视图的布局
    private func refreshFrame() {
        
        selectBtn.translatesAutoresizingMaskIntoConstraints = false
        selectBtn.topAnchor.constraint(equalTo: self.topAnchor,constant: 0).isActive = true
        selectBtn.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0).isActive = true
        selectBtn.leftAnchor.constraint(equalTo: self.leftAnchor,constant: 0).isActive = true
        selectBtn.widthAnchor.constraint(equalToConstant: 50).isActive = true
        
        allSelectLab.translatesAutoresizingMaskIntoConstraints = false
        allSelectLab.leftAnchor.constraint(equalTo: selectBtn.rightAnchor).isActive = true
        allSelectLab.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        allSelectLab.topAnchor.constraint(equalTo: self.topAnchor).isActive = true

        settlementBtn.translatesAutoresizingMaskIntoConstraints = false
        settlementBtn.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -10).isActive = true
        settlementBtn.heightAnchor.constraint(equalToConstant: 34).isActive = true
        settlementBtn.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        settlementBtn.widthAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true

        moneyLab.translatesAutoresizingMaskIntoConstraints = false
        moneyLab.rightAnchor.constraint(equalTo: settlementBtn.leftAnchor, constant: -20).isActive = true
        moneyLab.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        
        totalTagLab.translatesAutoresizingMaskIntoConstraints = false
        totalTagLab.rightAnchor.constraint(equalTo: moneyLab.leftAnchor, constant: -8).isActive = true
        totalTagLab.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        
        topLine.translatesAutoresizingMaskIntoConstraints = false
        topLine.topAnchor.constraint(equalTo: self.topAnchor,constant: 0).isActive = true
        topLine.rightAnchor.constraint(equalTo: self.rightAnchor,constant: 0).isActive = true
        topLine.leftAnchor.constraint(equalTo: self.leftAnchor,constant: 0).isActive = true
        topLine.heightAnchor.constraint(equalToConstant: 0.5).isActive = true
        
        bottomLine.translatesAutoresizingMaskIntoConstraints = false
        bottomLine.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0).isActive = true
        bottomLine.rightAnchor.constraint(equalTo: self.rightAnchor,constant: 0).isActive = true
        bottomLine.leftAnchor.constraint(equalTo: self.leftAnchor,constant: 0).isActive = true
        bottomLine.heightAnchor.constraint(equalToConstant: 0.5).isActive = true

    }
    
    
    override func layoutSubviews() {
        refreshFrame()
    }

}


extension ShoppingCarBottomView{
    /// 点击全选的
    @objc private func clickSelectBtn() {
        selectBtn.isSelected = !selectBtn.isSelected
        if touchAllSelectEvent != nil {
            touchAllSelectEvent!(selectBtn.isSelected)
        }
    }
    
    /// 结算按钮
    @objc private func clickSettlementBtn() {
        if touchSettlementEvent != nil {
            touchAllSelectEvent!(settlementBtn.isSelected)
        }
    }
    
    /// money会自动保留两位小数
    func refreshSelectValue(_ number: Int , money: Float){
        assert(number < 0 , "结算参数有误")
        assert(money < 0 , "钱数有误")
        settlementBtn.setTitle("结算:\(number)", for: .normal)
        settlementBtn.isSelected = number != 0
        
        moneyLab.text = String(format: "¥%02d", money)
    }
    
    
}
